package com.epam.tcs.actions;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentAction implements Action{

	@Override
	public void doExec(HttpServletRequest req, HttpServletResponse res) {
		
		String target = req.getRequestURI().substring(
				req.getContextPath().length());
		
		String[] tarr = target.split("/");
		
		try {
			if (tarr.length >= 3 && tarr[2].equals("practice")) {
				outputPage("practice/practice.jsp", req, res);			
			} else if (tarr.length >= 3 && tarr[2].equals("lecture")) {
				outputPage("lecture/lecture.jsp", req, res);
			} else {
				outputPage("lecture/lecture.jsp", req, res);
			}		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void outputPage(String aJSPName, HttpServletRequest aRequest,
			HttpServletResponse aResponse) throws IOException, ServletException {
		
		RequestDispatcher dispatcher = aRequest.getRequestDispatcher("/jsp/"
				+ aJSPName);
		dispatcher.forward(aRequest, aResponse);
	}
}
